 #include "write_blf.h"
 #include "ycblf.h"

class Config {
public:
	Config(const Config &) = delete;
	Config &operator=(Config &) = delete;
	Config(const Config &&) noexcept = delete;
	Config &operator=(Config &&) noexcept = delete;
	~Config() = default;

	static std::unique_ptr<Writeblf>& get_wblfptr(){
 		return Ins().wblfptr_;
	}

	static void create_wblfptr(const char* file_name){
 		std::lock_guard lock(Ins().mutex_);
 		Ins().wblfptr_ = std::make_unique<Writeblf>(file_name);
	}

	static void destroy_wblfptr() {
 		std::lock_guard lock(Ins().mutex_);
 		Ins().wblfptr_.reset();
 		Ins().wblfptr_ = nullptr;
	}
private:
 	Config()
 	{
     	wblfptr_ = nullptr;
 	}

 	static Config &Ins() {
     	static Config cfg;
     	return cfg;
 	}

 	std::unique_ptr<Writeblf> wblfptr_;
 	std::mutex mutex_{};
};

int32_t ACStartWriteBlf(const char* file_name) {
 	Config::create_wblfptr(file_name);
 	if (Config::get_wblfptr()->StartWriteBlf() < 0) {
     	return -1;
    }
 	return 0;
}

int32_t ACBlfWriteCanData(CanData frame) {
	if (Config::get_wblfptr() == nullptr) return -1;
 	if (Config::get_wblfptr()->BlfWriteCanData(frame) < 0 )
     	return -1;
 	return 0;
}

int32_t ACBlfWriteCanFdData(CanData frame) {
	if (Config::get_wblfptr() == nullptr) return -1;
 	if (Config::get_wblfptr()->BlfWriteCanfdData(frame) < 0)
     	return -1;
 	return 0;
}

int32_t ACBlfWriteFlexrayData(FlexrayData frame) {
	if (Config::get_wblfptr() == nullptr) return -1;
 	if (Config::get_wblfptr()->BlfWriteFlexrayData(frame) < 0)
     	return -1;
 	return 0;
}

int32_t ACEndWriteBlf() {
	if (Config::get_wblfptr() == nullptr) return -1;
 	Config::get_wblfptr()->EndWriteBlf();
 	Config::destroy_wblfptr();
 	return 0;
}

bool ACBlfFileStat() {
	if (Config::get_wblfptr())
		return Config::get_wblfptr()->FileStat();
	else
		return false;
}

